

import javax.swing.*; 
import javax.swing.event.*; 
import javax.swing.table.*; 
import java.awt.*;
import java.awt.event.*;
import java.io.*;
 
public class NutritionTableView extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1025897412756409065L;
	JTable table;
	Object[][] data;
	Detail[][] detail;
	
	String name;
	int retention;
	String description;
	boolean identifiable;
	
	public static final int RETENTION_CURRENT=0;
	public static final int RETENTION_PURPOSE=1;
	public static final int RETENTION_LAW=2;
	public static final int RETENTION_BUSINESS=3;
	public static final int RETENTION_INDEFINITE=4;
	
	static String[] columns=new String[]{"","","provide service and maintain site","marketing","telemarketing","profiling","","other companies","public forums"};
	static String[] rows=new String[]{"","   contact information","   cookie","   demographic information","   financial information","   health information","   preferences","   purchasing information","   social security number & gov't ID","   your activity on this site","   your location"};
	
	static ImageIcon dont   =new ImageIcon(NutritionTableView.class.getResource("resource/dont.png"),"0");
	static ImageIcon collect=new ImageIcon(NutritionTableView.class.getResource("resource/collect.png"),"1");
	static ImageIcon optin  =new ImageIcon(NutritionTableView.class.getResource("resource/optin.png"),"2");
	static ImageIcon optout =new ImageIcon(NutritionTableView.class.getResource("resource/optout.png"),"3");
	static ImageIcon col1   =new ImageIcon(NutritionTableView.class.getResource("resource/col1.png"));
	static ImageIcon col2   =new ImageIcon(NutritionTableView.class.getResource("resource/col2.png"));
	static ImageIcon col3   =new ImageIcon(NutritionTableView.class.getResource("resource/col3.png"));
	static ImageIcon col4   =new ImageIcon(NutritionTableView.class.getResource("resource/col4.png"));
	static ImageIcon col5   =new ImageIcon(NutritionTableView.class.getResource("resource/col5.png"));
	static ImageIcon col6   =new ImageIcon(NutritionTableView.class.getResource("resource/col6.png"));
	
	public NutritionTableView(){
		super(new FlowLayout(FlowLayout.CENTER));
	}
	public NutritionTableView(String name, int retention,String description, boolean identifiable){
		super(new FlowLayout(FlowLayout.CENTER));
		this.name=name;
		this.retention=retention;
		this.description=description;
		this.identifiable=identifiable;
	}
	
	public void config(){	
		
		detail=new Detail[11][9];
		for(int i=0;i<11;i++){
			for(int j=0;j<9;j++){
				detail[i][j]=new Detail();
			}
		}
		
		data=new Object[11][9];
		for(int i=0;i<11;i++){
			data[i][0]=rows[i];
			data[i][1]=rows[0];
			data[i][6]=rows[0];
		}

		data[0][2]=NutritionTableView.col1;
		data[0][3]=NutritionTableView.col2;
		data[0][4]=NutritionTableView.col3;
		data[0][5]=NutritionTableView.col4;
		data[0][7]=NutritionTableView.col5;
		data[0][8]=NutritionTableView.col6;
		
		for(int i=1;i<11;i++){
			for(int j=2;j<9;j++){
				if(j!=6){
				data[i][j]=NutritionTableView.dont;
				}
			}			
		}
		
		TableModel dataModel = new DefaultTableModel() { 
            public int getColumnCount() { return 9; } 
            public int getRowCount() { return 11;} 
            public Object getValueAt(int row, int col) {
            	return data[row][col];
            	} 
            public String getColumnName(int column) {return columns[column];} 
            public boolean isCellEditable(int row, int col) {
            	if(col==0 || row==0 || col==1 || col==6)
            		return false;
            	else return true;
	    	} 
            public void setValueAt(Object aValue, int row, int column) {data[row][column] = aValue; } 
            public Class getColumnClass(int col){
            	return ImageIcon.class;
            }
         };

         table=new JTable(11,9);
         table.setModel(dataModel);
         table.setRowHeight(50);
//         table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
         
         JComboBox comboBox = new JComboBox(); 
      	 comboBox.addItem(NutritionTableView.dont);
         comboBox.addItem(NutritionTableView.optin);
      	 comboBox.addItem(NutritionTableView.optout);
      	 comboBox.addItem(NutritionTableView.collect);
      	 
      	 comboBox.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int row=table.getSelectedRow();
				int column=table.getSelectedColumn();
				
				JPanel d=MainFrame.getInstance().details.dataType;
				JPanel p=MainFrame.getInstance().details.purpose;
				JPanel r=MainFrame.getInstance().details.recipient;
				if(((JComboBox)e.getSource()).getSelectedIndex()==0){
					((CardLayout)d.getLayout()).show(d,"space");
					((CardLayout)p.getLayout()).show(p,"space");
					((CardLayout)r.getLayout()).show(r,"space");
				}else{
					((CardLayout)d.getLayout()).show(d,row+"");
					if(column<6){
						((CardLayout)p.getLayout()).show(p,column+"");
						((CardLayout)r.getLayout()).show(r,"space");
					}else{
						((CardLayout)r.getLayout()).show(r,column+"");
						((CardLayout)p.getLayout()).show(p,"space");
					}
					MainFrame.getInstance().details.refresh(detail[row][column]);
				}
			}
      		 
      	 });
      	 //**********************************///
      	 comboBox.addItemListener(new ItemListener(){
			public void itemStateChanged(ItemEvent e) {
				int index=MainFrame.getInstance().tabPan.getSelectedIndex();
				PolicyFrame pf=MainFrame.getInstance().policies.get(index);
				if(!pf.changed){
					pf.changed=true;
					String title=pf.projectName;
					if(title==""){
						title=MainFrame.getInstance().tabPan.getTitleAt(index);
					}
					title="*"+title;
					MainFrame.getInstance().tabPan.setTitleAt(index, title);
				}
			}  		 
      	 });
      	 
      	 
      	DefaultTableCellRenderer ImageRender = new DefaultTableCellRenderer() { 
    	    public void setValue(Object value) { 
    	        if (value instanceof ImageIcon) { 
    	        	ImageIcon c = (ImageIcon) value; 
    	            setIcon(c);
    		} else { 
    			if (value instanceof String) { 
     	        	setText((String)value);
     		}else
    		    super.setValue(value); 
    		} 
    	    } 
         };
         
         DefaultTableCellRenderer TextRender = new DefaultTableCellRenderer() { 
     	    public void setValue(Object value) { 
     	        if (value instanceof String) { 
     	        	setText((String)value);
     		} else { 
     		    super.setValue(value); 
     		} 
     	    } 
          };
      	 
      	TableColumnModel tcm = table.getColumnModel();  
      	tcm.getColumn(0).setCellRenderer(TextRender);
      	tcm.getColumn(0).setPreferredWidth(200);
  		tcm.getColumn(0).setMaxWidth(200);
  		tcm.getColumn(0).setMinWidth(200);
  		tcm.getColumn(1).setCellRenderer(TextRender);
      	tcm.getColumn(1).setPreferredWidth(10);
  		tcm.getColumn(1).setMaxWidth(10);
  		tcm.getColumn(1).setMinWidth(10);
  		tcm.getColumn(6).setCellRenderer(TextRender);
      	tcm.getColumn(6).setPreferredWidth(10);
  		tcm.getColumn(6).setMaxWidth(10);
  		tcm.getColumn(6).setMinWidth(10);
      	for(int i=2;i<9;i++){
      		if(i!=6){
      		tcm.getColumn(i).setCellRenderer(ImageRender);   
      		tcm.getColumn(i).setCellEditor(new DefaultCellEditor(comboBox));
      		tcm.getColumn(i).setPreferredWidth(80);
      		tcm.getColumn(i).setMaxWidth(80);
      		tcm.getColumn(i).setMinWidth(80);
      		}
      	}
      	
      	JPanel pan=new JPanel();
      	pan.setLayout(new BorderLayout());
      	pan.add(table,"Center");
      	JLabel label=new JLabel("<html> <body> <br> &nbsp &nbsp  " +
      			"information we collect " +
      			"&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp " +
      			"ways we use your information" +
      			" &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp " +
      			"information sharing" +
      			"<br><br></body></html>");

      	pan.add(label,"North");
      	add(pan);
     }

	public Object[][] getData() {
		return data;
	}
	
	public Detail[][] getDetail(){
		return detail;
	}
	
	public void setData(Object[][] data) {
		this.data = data;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getRetention() {
		return retention;
	}
	public void setRetention(int retention) {
		this.retention = retention;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public boolean isIdentifiable() {
		return identifiable;
	}
	public void setIdentifiable(boolean identifiable) {
		this.identifiable = identifiable;
	}
	
}

class Detail implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1020667084585051030L;
	boolean[] bool;
	public Detail(){
		bool=new boolean[33];
	}
	public void set(int i,boolean b){
		if(i>=0&&i<33){
			bool[i]=b;
		}
	}
	public boolean isTrue(int i){
		if(i>=0&&i<33){
			return bool[i];
		}else{
			return false;
		}
	}
}
